package com.alai04.android.scoreboard;

import java.util.HashSet;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PlayerNames {
	private static HashSet<String> names = new HashSet<String>();
	private SQLiteDatabase db = null;

	public PlayerNames(Context context) {
		db = (new ScoreDBHelper(context)).getWritableDatabase();
		String sql = "SELECT " + ScoreDBHelper.NAME + " FROM "
				+ ScoreDBHelper.TABLE_NAMES;
		Cursor result = db.rawQuery(sql, null);
		result.moveToFirst();
		while (!result.isAfterLast()) {
			names.add(result.getString(0));
			result.moveToNext();
		}
		result.close();
	}

	protected void finalize() throws Throwable
	{
	  if (db != null)
		  db.close();
	} 
	
	public void add(String s) {
		if (s.length() > 0 && names.add(s)) {
			ContentValues values = new ContentValues(1);
			values.put(ScoreDBHelper.NAME, s);
			db.insert(ScoreDBHelper.TABLE_NAMES, ScoreDBHelper.NAME, values);
		}
	}

	public String[] toArray() {
		return names.toArray(new String[0]);
	}
}
